// 导入微信登录、微信绑定用户、用户信息、用户信息组和当前商家的API
import {
	passwordLogin,
	wechatLogin,
	wechatBindUser,
	apiUserInfo,
	apiUserInfoGroups,
	walletRechargeRecord,
	walletRecord,
	walletRecharge,
} from "@/common/apis/user";
import {
	merchtantsCurrent
} from "@/common/apis/store.js";
import {
	currentSupplier
} from "@/common/apis/supplier.js";
import {
	currentAgent
} from "@/common/apis/agent.js";

const tabbar = [{
		pagePath: "/pages/index/index",
		iconPath: "/static/images/tabbar/home.png",
		selectedIconPath: "/static/images/tabbar/home_1.png",
		text: "首页",
	},
	{
		pagePath: "/pages/index/categorize/categorize",
		iconPath: "/static/images/tabbar/categorize.png",
		selectedIconPath: "/static/images/tabbar/categorize_1.png",
		text: "找货",
	},
	{
		pagePath: "/pages/index/replenish",
		iconPath: "/static/images/tabbar/brand.png",
		selectedIconPath: "/static/images/tabbar/brand_1.png",
		text: "补货",
	},
	{
		pagePath: "/pages/index/cart",
		iconPath: "/static/images/tabbar/cart.png",
		selectedIconPath: "/static/images/tabbar/cart_1.png",
		text: "进货单",
	},
	{
		pagePath: "/pages/index/member",
		iconPath: "/static/images/tabbar/member.png",
		selectedIconPath: "/static/images/tabbar/member_1.png",
		text: "我的",
	},
];

/**
 * 用户模块的状态管理
 * @module user
 */
export default {
	/**
	 * 定义模块的初始状态
	 * @returns {Object} 包含用户状态的对象
	 */
	state() {
		return {
			// 从本地存储中获取登录状态
			isLogin: uni.getStorageSync("isLogin"),
			// 从本地存储中获取用户令牌
			token: uni.getStorageSync("token"),
			// 从本地存储中获取用户信息
			userInfo: uni.getStorageSync("USERINFO"),
			// 从本地存储中获取用户角色信息
			userGroup: uni.getStorageSync("USERGROUP"),
			// 从本地存储中获取店铺信息
			storeInfo: uni.getStorageSync("STOREINFO"),
			// 从本地存储中获取店铺状态，默认为false
			isStore: uni.getStorageSync("isStore") || false,
			// 从本地存储中获取供应商信息
			supplierInfo: uni.getStorageSync("SUPPLIERINFO"),
			// 从本地存储中获取供应商状态，默认为false
			isSupplier: uni.getStorageSync("isSupplier") || false,
			// 从本地存储中获取代理商信息
			agentInfo: uni.getStorageSync("AGENTINFO"),
			// 从本地存储中获取代理商状态，默认为false
			isAgent: uni.getStorageSync("isAgent") || false,
			// 用户角色列表
			roles: uni.getStorageSync("roles") || [],
			// 当前选中的角色Id
			currentRoleId: uni.getStorageSync("currentRoleId") || 0,
			// 底部导航栏配置
			tabbar: uni.getStorageSync("tabbar") || tabbar,
		};
	},

	mutations: {
		SET_TABBAR(state, list) {
			state.tabbar = list;
			uni.setStorageSync("tabbar", state.tabbar);
		},
		SET_CURRENT_ROLE(state, roleId) {
			switch (roleId) {
				case 1: // 商家
					this.commit("SET_TABBAR", tabbar);
					break;
				case 2: // 供应商
					this.commit("SET_TABBAR", [{
							pagePath: "/pages/supplier/statistics",
							iconPath: "/static/images/tabbar/home.png",
							selectedIconPath: "/static/images/tabbar/home_1.png",
							text: "工作台",
						},
						{
							pagePath: "/pages/supplier/goods",
							iconPath: "/static/images/tabbar/categorize.png",
							selectedIconPath: "/static/images/tabbar/categorize_1.png",
							text: "商品管理",
						},
						{
							pagePath: "/pages/supplier/member",
							iconPath: "/static/images/tabbar/member.png",
							selectedIconPath: "/static/images/tabbar/member_1.png",
							text: "我的",
						},
					]);
					break;
				case 3: // 代理商
					this.commit("SET_TABBAR", [{
							pagePath: "/pages/agenter/statistics",
							iconPath: "/static/images/tabbar/home.png",
							selectedIconPath: "/static/images/tabbar/home_1.png",
							text: "工作台",
						},
						{
							pagePath: "/pages/agenter/member",
							iconPath: "/static/images/tabbar/member.png",
							selectedIconPath: "/static/images/tabbar/member_1.png",
							text: "我的",
						},
					]);
					break;
				default:
					this.commit("SET_TABBAR", tabbar);
					break;
			}
			state.currentRoleId = roleId;
			uni.setStorageSync("tabbar", state.tabbar);
			uni.setStorageSync("currentRoleId", roleId);
		},
	},

	actions: {
		async fetchPasswordLogin({
			state,
			dispatch
		}, data) {
			const response = await passwordLogin(data);
			if (response) {
				// 更新状态中的令牌和登录状态
				state.token = response.access;
				state.isLogin = true;
				// 将令牌和登录状态存储到本地存储
				uni.setStorageSync("token", state.token);
				uni.setStorageSync("isLogin", true);
				// 调用获取用户信息和用户角色信息的action
				await dispatch("fetchGetUserInfo");
				await dispatch("fetchGetUserGroups");
			}
		},
		/**
		 * 微信手机号绑定用户
		 * @param {Object} state - 当前状态
		 * @param {Object} data - 包含微信绑定用户所需的数据
		 * @returns {Promise} 返回微信绑定用户的响应
		 */
		async fetchWechatBindUser(state, data) {
			const response = await wechatBindUser(data);
			return response;
		},

		/**
		 * 微信小程序一键登录
		 * @param {Object} context - 包含state和dispatch的上下文对象
		 * @param {Object} data - 包含微信登录所需的数据
		 * @returns {Promise} 返回微信登录的响应
		 */
		async fetchWechatLogin({
			state,
			dispatch
		}, data) {
			const response = await wechatLogin(data);
			if (response) {
				// 更新状态中的令牌和登录状态
				state.token = response.access;
				state.isLogin = true;
				// 将令牌和登录状态存储到本地存储
				uni.setStorageSync("token", state.token);
				uni.setStorageSync("isLogin", true);
				// 调用获取用户信息和用户角色信息的action
				await dispatch("fetchGetUserInfo");
				await dispatch("fetchGetUserGroups");
			}
			return response;
		},

		/**
		 * 获取用户信息
		 * @param {Object} context - 包含state的上下文对象
		 * @returns {Promise} 返回用户信息的响应
		 */
		async fetchGetUserInfo({
			state
		}) {
			const response = await apiUserInfo();
			if (response) {
				// 更新状态中的用户信息
				state.userInfo = response;
				// 将用户信息存储到本地存储
				uni.setStorageSync("USERINFO", response);
			}
			return response;
		},

		/**
		 * 获取用户角色信息
		 * @param {Object} context - 包含state的上下文对象
		 * @returns {Promise} 返回用户角色信息的响应
		 */
		async fetchGetUserGroups({
			state,
			commit
		}) {
			const response = await apiUserInfoGroups();
			if (response.length > 0) {
				// 更新状态中的用户角色信息
				state.userGroup = response;
				state.roles = response;
				if (response && response.length && state.currentRoleId == 0) {
					commit("SET_CURRENT_ROLE", response[0].id);
				}
				// 将用户角色信息存储到本地存储
				uni.setStorageSync("USERGROUP", response);
			}
			return response;
		},

		/**
		 * 获取当前商家信息
		 * @param {Object} context - 包含state的上下文对象
		 * @returns {Promise} 返回当前商家信息的响应
		 */
		async fetchMerchtantsCurrent({
			state
		}) {
			const response = await merchtantsCurrent();
			if (response && response.data && response.data.info) {
				// 更新状态中的店铺信息和店铺状态
				state.storeInfo = response.data.info;
				state.isStore =
					response.data.info && response.data.info.status == "active";
				// 将店铺信息和店铺状态存储到本地存储
				uni.setStorageSync("isStore", state.isStore);
				uni.setStorageSync("STOREINFO", response.data.info);
			}
			return response;
		},

		/**
		 * 获取当前供应商信息
		 * @param {Object} context - 包含state的上下文对象
		 * @returns {Promise} 返回当前供应商信息的响应
		 */
		async fetchSupplierCurrent({
			state
		}) {
			const response = await currentSupplier();
			if (response && response.data && response.data.info) {
				// 更新状态中的店铺信息和店铺状态
				state.supplierInfo = response.data.info;
				state.isSupplier = response.data.is_supplier;
				// 将店铺信息和店铺状态存储到本地存储
				uni.setStorageSync("isSupplier", state.isSupplier);
				uni.setStorageSync("SUPPLIERINFO", response.data.info);
			}
			return response;
		},

		async fetchAgentCurrent({
			state
		}) {
			const response = await currentAgent();
			if (response && response.data && response.data.info) {
				// 更新状态中的店铺信息和店铺状态
				state.agentInfo = response.data.info;
				state.isAgent = response.data.is_agent;
				uni.setStorageSync("isAgent", state.isAgent);
				uni.setStorageSync("AGENTINFO", response.data.info);
			}
		},

		/**
		 * 退出登录
		 * @param {Object} context - 包含state的上下文对象
		 */
		logout({
			state
		}) {
			// 清除状态中的登录状态、令牌、用户信息、店铺信息和店铺状态
			state.isLogin = false;
			state.token = "";
			state.userInfo = "";
			state.storeInfo = "";
			state.isStore = false;
			state.currentRoleId = 0;
			state.tabbar = tabbar;
			// 清除本地存储中的所有数据
			uni.clearStorageSync();
			// 移除标签栏上的徽章
			uni.removeTabBarBadge({
				index: 3,
			});
			this.commit("SET_TABATAR", tabbar);
			// 重新加载登录页面
			uni.reLaunch({
				url: "/pages/user/login",
			});
		},

		/**
		 * 获取钱包充值记录
		 * @param {Object} context - 包含state的上下文对象
		 * @param {Object} payload - 包含获取钱包记录所需的数据
		 * @returns {Promise} 返回钱包记录的响应
		 */
		async fetchWalletRechargeRecord({}, payload) {
			const response = await walletRechargeRecord(payload);
			if (response) {
				return Promise.resolve(response);
			}
			return Promise.reject(response);
		},

		/**
		 * 获取钱包余额变更记录
		 * @param {Object} context - 包含state的上下文对象
		 * @param {Object} payload - 包含获取钱包记录所需的数据
		 * @returns {Promise} 返回钱包记录的响应
		 */
		async fetchWalletRecord({}, payload) {
			const response = await walletRecord(payload);
			if (response) {
				return Promise.resolve(response);
			}
			return Promise.reject(response);
		},

		/**
		 * 钱包充值
		 * @param {Object} context - 包含state的上下文对象
		 * @param {Object} payload - 包含获取钱包记录所需的数据
		 * @returns {Promise} 返回钱包充值的响应
		 */
		async fetchWalletRecharge({}, payload) {
			const response = await walletRecharge(payload);
			if (response) {
				return Promise.resolve(response);
			}
			return Promise.reject(response);
		},
	},
};